package com.mo.common.lucene.utils;

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.DoublePoint;
import org.apache.lucene.document.IntPoint;
import org.apache.lucene.document.LongPoint;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.ScoreDoc;

public class DocAndBeanUtils {
	/**
     * 将SolrDocument转换成Bean
     * @param record
     * @param clazz
     * @return
     */
    public static Object toBean(Document record, Class clazz){
        Object obj = null;
        try {
            obj = clazz.newInstance();
        } catch (InstantiationException e1) {
            e1.printStackTrace();
        } catch (IllegalAccessException e1) {
            e1.printStackTrace();
        }
        Field[] fields = clazz.getDeclaredFields();//();
        for(Field field:fields){
            Object value = record.get(field.getName());
            try {
                BeanUtils.setProperty(obj, field.getName(), value);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
        return obj;
    }

   

/**
     * 将SolrDocumentList转换成BeanList
     * @param records
     * @param clazz
     * @return
     */
    public static Object toBeanList(IndexSearcher   indexSearcher, ScoreDoc[] records, Class clazz){
        List list = new ArrayList();
        for(ScoreDoc scoreDoc : records){
        	 Document document;
			try {
				document = indexSearcher.doc(scoreDoc.doc);
				list.add(toBean(document,clazz));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
        return list;
    }
    public static Document toDocument( Document document, Object objectBean) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
//    	 Document document = new Document();
    	 Field[] fields = objectBean.getClass().getDeclaredFields();//();
    	// 遍历所有属性  
         for (int j = 0; j < fields.length; j++) {  
             // 获取属性的名字  
             String name = fields[j].getName();  
             // 将属性的首字符大写，方便构造get，set方法  
             String UInitialname = name.substring(0, 1).toUpperCase() + name.substring(1);  
             // 获取属性的类型  
             String type = fields[j].getGenericType().toString();  
             // 如果type是类类型，则前面包含"class "，后面跟类名  
//             System.out.println("属性为：" + name);  
             if (type.equals("class java.lang.String")) {  
                 Method m = objectBean.getClass().getMethod("get" + UInitialname);  
                 // 调用getter方法获取属性值  
                 String value = (String) m.invoke(objectBean);  
//                 System.out.println("数据类型为：String");  
                 if (value != null) {  
//                	 System.out.println("属性值为：" + value);  
                	 /*if(value.length()>10){
                		 document.add(new TextField(name,  value, org.apache.lucene.document.Field.Store.YES));
                	 }else{
                	 }*/
                	 document.add(new StringField(name,  value, org.apache.lucene.document.Field.Store.YES));
                 } else {  
                 	System.out.println("属性值为：空");  
                 }  
             }  
             if (type.equals("class java.lang.Integer")) {  
                 Method m = objectBean.getClass().getMethod("get" + UInitialname);  
                 Integer value = (Integer) m.invoke(objectBean);  
//                 System.out.println("数据类型为：Integer");  
                 if (value != null) {  
//                	 System.out.println("属性值为：" + value);  
                	 document.add(new IntPoint(name,  value));
                 } else {  
                     System.out.println("属性值为：空");  
                 }  
             }  
             if (type.equals("class java.lang.Short")) {  
                 Method m = objectBean.getClass().getMethod("get" + UInitialname);  
                 Short value = (Short) m.invoke(objectBean);  
//                 System.out.println("数据类型为：Short");  
                 if (value != null) {  
//                     System.out.println("属性值为：" + value);
            		 document.add(new StringField(name,  value.toString(), org.apache.lucene.document.Field.Store.YES));
                 } else {  
//                     System.out.println("属性值为：空");  
                 }  
             }  
             if (type.equals("class java.lang.Double")) {  
                 Method m = objectBean.getClass().getMethod("get" + UInitialname);  
                 Double value = (Double) m.invoke(objectBean);  
//                 System.out.println("数据类型为：Double");  
                 if (value != null) {  
//                     System.out.println("属性值为：" + value); 
                     document.add(new DoublePoint(name,  value));
                 } else {  
//                     System.out.println("属性值为：空");  
                 }  
             }  
             if (type.equals("class java.lang.Boolean")) {  
                 Method m = objectBean.getClass().getMethod("get" + UInitialname);  
                 Boolean value = (Boolean) m.invoke(objectBean);  
//                 System.out.println("数据类型为：Boolean");  
                 if (value != null) {  
//                     System.out.println("属性值为：" + value);  
                     document.add(new StringField(name,  value.toString(), org.apache.lucene.document.Field.Store.YES));
                 } else {  
//                     System.out.println("属性值为：空");  
                 }  
             }  
             if (type.equals("class java.util.Date")) {  
             	 Method m = objectBean.getClass().getMethod("get" + UInitialname);  
                 Date value = (Date) m.invoke(objectBean);  
//                 System.out.println("数据类型为：Date");  
                 if (value != null) {  
                	 document.add(new StringField(name,  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(value), org.apache.lucene.document.Field.Store.YES));
                	 document.add(new LongPoint(name+"Long",  value.getTime()));
//                     System.out.println("属性值为：" + value);  
                 } else {  
//                     System.out.println("属性值为：空");  
                 }  
             }else{
            	/* Method m = objectBean.getClass().getMethod("get" + UInitialname);  
            	 Object value = m.invoke(objectBean);  
                 System.out.println("数据类型为：Object");  
                 if (m.invoke(objectBean) != null) {  
                	 document.add(new StringField(name,  value.toString(), org.apache.lucene.document.Field.Store.YES));
                     System.out.println("属性值为：" + value);  
                 } else {  
                     System.out.println("属性值为：空");  
                 } */ 
             }  
         }  
    	return document;
    }
}
